package com.JZ.QM.controller;

import com.JZ.QM.common.entity.CombinationDetailInfoEntity;
import com.JZ.QM.common.utils.CommonResult;
import com.JZ.QM.feign.CombinationDynamicInfoFeignService;
import com.JZ.QM.feign.CombinationsFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author: cyc
 * @email cyc@gmail.com
 * @date: 2020/7/3 8:50
 */
@RestController
@RefreshScope
@Slf4j
@RequestMapping(value="/combinations")
public class CombinationsController {

    @Resource
    CombinationsFeignService combinationsFeignService;
    @Resource
    CombinationDynamicInfoFeignService combinationDynamicInfoFeignService;

    /**
     * 返回所有的组合列表
     * @param riskLevel 组合的风险等级
     * @return
     */
    @RequestMapping("/getCombinationsList")
    public CommonResult getCombinationDetailList(Integer riskLevel){
        //组合详情列表
        ArrayList<LinkedHashMap> combinationDetailInfoEntities=new ArrayList<>();
        combinationDetailInfoEntities= (ArrayList<LinkedHashMap>) combinationsFeignService.getCombinationList(null,riskLevel).get("combinationList");
        if (combinationDetailInfoEntities == null){
            return CommonResult.success("组合产品数据为空").put("combinationList",combinationDetailInfoEntities);
        } else {
            return CommonResult.success().put("combinationList",combinationDetailInfoEntities.toArray());
        }
    }

    /**
     * 更具累计收益来获得组合收益排行
     * @return
     */
    @RequestMapping("/getCombinationDetailListByIncome")
    public CommonResult getCombinationDetailListByIncome(){
        //组合详情列表
        ArrayList<LinkedHashMap> combinationDetailInfoEntities=new ArrayList<>();
        combinationDetailInfoEntities= (ArrayList<LinkedHashMap>) combinationsFeignService.getCombinationListByIncome().get("combinationList");

        if (combinationDetailInfoEntities == null){
            return CommonResult.success("组合产品数据为空").put("combinationList",combinationDetailInfoEntities);
        } else {
            return CommonResult.success().put("combinationList",combinationDetailInfoEntities.toArray());
        }


    }

    /**
     * 更新组合访问量
     * @param comBaseId 组合id
     * @return
     */
    @RequestMapping("/updateComVisitNum")
    public CommonResult updateComVisitNum(Long comBaseId){
        //访问量更新状态
        Integer code=0;

        //更新访问量
        code=combinationDynamicInfoFeignService.updateCombinationDynamicInfo(comBaseId,0,1,0,0).getCode();

        if(code == 200 ){
            return CommonResult.success("访问量更新成功");
        } else {
            return CommonResult.error(201,"访问量更新失败");
        }
    }
}
